import tkinter as tk
from tkinter import ttk
# import threading
from new import ContourPlotter


class ContourApp:

    def __init__(self, root):
        self.root = root
        self.root.title("等高线图生成器")
        self.root.geometry("400x250")

        # 创建主框架
        main_frame = ttk.Frame(root, padding="20")
        main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))

        # 标题
        title_label = ttk.Label(main_frame,
                                text="等高线图生成工具",
                                font=("Arial", 16, "bold"))
        title_label.grid(row=0, column=0, columnspan=2, pady=(0, 20))

        # 说明文本
        info_label = ttk.Label(main_frame,
                               text="点击下方按钮开始生成等高线图",
                               font=("Arial", 10))
        info_label.grid(row=1, column=0, columnspan=2, pady=(0, 20))

        # 开始按钮
        self.start_button = ttk.Button(main_frame,
                                       text="开始生成等高线图",
                                       command=self.start_contour_plot)
        self.start_button.grid(row=2, column=0, padx=10, pady=10)

        # 体积计算按钮
        volume_button = ttk.Button(main_frame,
                                   text="计算两CSV文件体积差",
                                   command=self.calculate_volume_difference)
        volume_button.grid(row=2, column=1, padx=10, pady=10)

        # 退出按钮
        quit_button = ttk.Button(main_frame,
                                 text="退出",
                                 command=self.safe_destroy)
        quit_button.grid(row=3, column=0, columnspan=2, padx=10, pady=10)

        # 状态标签
        self.status_label = ttk.Label(main_frame, text="", font=("Arial", 9))
        self.status_label.grid(row=4, column=0, columnspan=2, pady=(20, 0))

    def safe_destroy(self):
        # 安全地关闭窗口
        try:
            self.root.quit()
            self.root.destroy()
        except Exception as e:
            import sys
            sys.exit(0)

    def start_contour_plot(self):
        # 直接在主线程中运行等高线图，避免线程问题
        self.start_button.config(state="disabled")
        self.status_label.config(text="正在加载数据和生成等高线图...")

        # 使用after方法延迟执行，确保UI更新完成后再运行
        self.root.after(100, self.run_contour_plot)

    def calculate_volume_difference(self):
        # 计算两个CSV文件的体积差
        self.start_button.config(state="disabled")
        self.status_label.config(text="正在计算两个CSV文件的体积差...")

        # 使用after方法延迟执行，确保UI更新完成后再运行
        self.root.after(100, self.run_volume_calculation)

    def run_contour_plot(self):
        try:
            plotter = ContourPlotter()
            plotter.run()
        except Exception as e:
            print(f"运行出错: {e}")
        finally:
            self.reset_ui()

    def run_volume_calculation(self):
        try:
            plotter = ContourPlotter()
            plotter.calculate_volume_difference()
        except Exception as e:
            print(f"运行出错: {e}")
        finally:
            self.reset_ui()

    def reset_ui(self):
        # 重置UI状态
        try:
            self.start_button.config(state="normal")
            self.status_label.config(text="")
        except:
            pass


if __name__ == "__main__":
    root = tk.Tk()
    app = ContourApp(root)
    root.mainloop()
